﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace csharpTestBrowser
{
    public partial class frmBrowser : Form
    {
        //constants
        const int MAX_TABS = 5;
        const int MAX_BOOKMARKS = 10;

        //variables
        WebBrowser[] browserDB;
        string[] arrBookmarks;
        MenuItem mnu = new MenuItem();


        public frmBrowser()
        {
            InitializeComponent();
        }

        //
        //navigation buttons
        //
        private void btnBack_Click(object sender, EventArgs e)
        {
            this.browserDB[browserTabs.SelectedIndex].GoBack();
        }

        private void btnForward_Click(object sender, EventArgs e)
        {
            this.browserDB[browserTabs.SelectedIndex].GoForward();
        }

        public void btnGo_Click(object sender, EventArgs e)
        {
            this.browserDB[browserTabs.SelectedIndex].Navigate(cbURL.Text);
            ssLblStatus.Text = this.browserDB[browserTabs.SelectedIndex].StatusText;
        }

        private void btnHome_Click(object sender, EventArgs e)
        {
            this.browserDB[browserTabs.SelectedIndex].GoHome();
            ssLblStatus.Text = this.browserDB[browserTabs.SelectedIndex].StatusText;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.browserDB[browserTabs.SelectedIndex].Refresh();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            this.browserDB[browserTabs.SelectedIndex].Stop();
        }

        //
        //WebBrowser events
        //
        private void finishedLoading(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //update tab title
            browserTabs.SelectedTab.Text = this.browserDB[browserTabs.SelectedIndex].DocumentTitle;

            cbURL.Text = this.browserDB[browserTabs.SelectedIndex].Url.ToString();

            ssLblStatus.Text = this.browserDB[browserTabs.SelectedIndex].StatusText;

        }

        private void FirstStart(object sender, EventArgs e)
        {
            this.cbURL.Select();

            this.browserDB = new WebBrowser[MAX_TABS];

            for (int i = 0; i < MAX_TABS; i++)
                this.browserDB[i] = new WebBrowser();

            this.browserDB[0] = browserTab1;
            this.browserDB[1] = browserTab2;
            this.browserDB[2] = browserTab3;
            this.browserDB[3] = browserTab4;
            this.browserDB[4] = browserTab5;

        }

        //
        //Menu bar
        //
        private void mnuClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private MenuItem AddMenuItem(String title)
        {
            // create a new menuitem
            MenuItem mi = new MenuItem(title);
            // set as Delegate for the Click Event of this MenuItem
            mi.Click += new
              System.EventHandler(this.mnuBookMarksBookMarks_Click);

            // Ownerdraw is needed only if you want to
            // add Icons and custom highlighting etc
            mi.OwnerDraw = true;
            // ..._MeasureItem, ..._DrawItem Virtual function are called
            // by the system to render the items individually
            // every time a visual aspect changes
            mi.DrawItem += new
              System.Windows.Forms.DrawItemEventHandler
              (this.mnuBookMarksBookMarks_DrawItem);
            mi.MeasureItem += new
              System.Windows.Forms.MeasureItemEventHandler
              (this.mnuBookMarksBookMarks_MeasureItem);
            // ..._DrawItem and ..._MeasureItem EventHandler Delegates 
            // for each MenuItem are created above
            return mi;
        }

        private void mnuBookMarksBookMarks_DrawItem(object sender,
             System.Windows.Forms.DrawItemEventArgs e)
        {
            // unfortunately only 2 states are passed to this function
            // NoAccelerator
            // Selected, NoAccelerator

            SolidBrush brush;
            if (e.State.Equals(DrawItemState.Selected |
                            DrawItemState.NoAccelerator))
            {
                // Item is Selected highlight the Item
                Pen blackPen = new Pen(Color.Black, 1);
                e.Graphics.DrawRectangle(blackPen, e.Bounds);
                brush = new SolidBrush(Color.FromArgb(40, 10, 10, 255));
                // create a highlight rectangle
                e.Graphics.FillRectangle(brush, e.Bounds);
                return;
            }

            if (e.State.Equals(DrawItemState.NoAccelerator))
            {
                // Item is not selected, remove highlight rectangle
                // created above
                Pen whitePen = new Pen(Color.White, 1);
                e.Graphics.DrawRectangle(whitePen, e.Bounds);
                SolidBrush brushErase = new SolidBrush(Color.White);
                e.Graphics.FillRectangle(brushErase, e.Bounds);

                MenuItem item = (MenuItem)sender;

                // get the MenuItem's Rectangle
                Rectangle rect = e.Bounds;

                SolidBrush drawBrush = new SolidBrush(Color.Black);
                RectangleF drawRect = new RectangleF(e.Bounds.X + 20,
                      e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);
                brush = new SolidBrush(Color.FromArgb(236, 233, 216));
                // draw a transparent rectangle over the Image
                // else the Image will not be visible
                e.Graphics.FillRectangle(brush,
                      rect.Left, rect.Top, 18, rect.Bottom);

                // Draw MenuItem Text , you can use some funcky 
                // Font for this purpose
                e.Graphics.DrawString(item.Text, e.Font,
                                      drawBrush, drawRect);

                // Render the Image (Icon)
                // i have used a ImageList 
                imageList1.Draw(e.Graphics, rect.Left + 1, rect.Top, 14, 14, 0);
            }
        }

        private void mnuBookMarksBookMarks_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
        {
            // retreive the MenuItem in question
            // note : ..._MeasureItem is called before ..._DrawItem
            MenuItem item = (MenuItem)sender;
            // we set the height of each item to 20
            // remember the icons are 16x16
            e.ItemHeight = 20;
            SizeF stringSize = new SizeF();
            stringSize = e.Graphics.MeasureString(item.Text, this.Font);
            // set the width of the item based on the 
            // length of the prompt
            e.ItemWidth = (int)stringSize.Width;
        }

        //Bookmarks Menu
        private void BookmarksLoad(object sender, EventArgs e)
        {
            string data;
            arrBookmarks = new string[MAX_BOOKMARKS];
            MenuItem mi = AddMenuItem(data);

            mnu.MenuItems.Add(mi);
        }

        private void BookmarksManage(object sender, EventArgs e)
        {

        }

        private void refreshTab(object sender, TabControlEventArgs e)
        {
            try
            {
                cbURL.Text = this.browserDB[browserTabs.SelectedIndex].Url.ToString();
                ssLblStatus.Text = this.browserDB[browserTabs.SelectedIndex].StatusText;
            }
            catch
            {
                cbURL.Text = "";
                ssLblStatus.Text = "No URL.";
            }
        }

        private void mnuBookMarksBookMarks_Click(object sender, EventArgs e)
        {

        }
              
    }
}

/* OLD
        private void setHomePageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            objFormHome = new FormSetHome("fix this...");
            objFormHome.Show();
        }*/

